package com.gxa.hualian.service.impl;

import com.gxa.hualian.dao.UserDao;
import com.gxa.hualian.dao.impl.UserDaoImpl;
import com.gxa.hualian.entity.User;
import com.gxa.hualian.exception.BusinessException;
import com.gxa.hualian.exception.ExceptionCode;
import com.gxa.hualian.service.UserService;


import java.time.ZonedDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author bill
 * @date 2022/4/29 15:39
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();

    /**
     * 判断被注册的手机号是否存在(select -> ==UserDao( selectByPhone)==) ,如果存在注册失败
     * 如果不存在( insert ->==UserDao( insert)==) )
     *
     * @param user User对象(没有id)
     * @return
     */
    @Override
    public User register(User user) throws Exception {

        // 查询该手机号是否存在
        List<User> users = userDao.selectByPhone(user.getPhone());
        // 存在则不能注册, 返回null
        // 升级: 用自定义异常来实现消息的传递
        if (users.size() > 0) {
            // return null;
//            throw new BusinessException("手机号重复");
            throw new BusinessException(ExceptionCode.DUPLICATE_PHONE);
        }


        // 当满足手机号不存在,可以注册的时候
        // 对密码进行加密
//        user.setPwd(MD5Util.encode(user.getPwd()));

        // 注册时间,使用系统当前时间生成
        // ctrl+p 参数提示快捷键
//        user.setRegTime(new Date(System.currentTimeMillis()));
        Date date = Date.from(ZonedDateTime.now().toInstant());
        user.setRegTime(date);
        // 再给dao层传递user对象的时候,密码字段是已经加密后的
        Integer insert = userDao.insert(user);
        // 获取注册成功的id
        List<User> selectUserList = userDao.selectByPhone(user.getPhone());
        return selectUserList.get(0);
    }

    @Override
    public Boolean login(User user) throws Exception {
        List<User> users = userDao.selectByPhone(user.getPhone());
        if (users.size() > 0) {
            return true;
        }
        throw new BusinessException(ExceptionCode.DUPLICATE_PHONE);


    }

    @Override
    public List<User> sellectAll() throws Exception {
        List<User> lists= userDao.selectAll();
        return lists;
    }

    @Override
    public Boolean deleteById(Integer id) throws Exception {
        Boolean success = userDao.deleteById(id);
        if (!success){
            throw new BusinessException(ExceptionCode.WRONG_ID);
        }
        return success;
    }

    @Override
    public Boolean updateById(User user) throws Exception {
        Date date = Date.from(ZonedDateTime.now().toInstant());
        user.setRegTime(date);
        Integer update = userDao.update(user);
        if (update>0){
            return false;
        }
        throw new BusinessException(ExceptionCode.NONE_ID);
    }

    @Override
    public List<User> selectByPage(Integer page, Integer limit) throws Exception {
        return userDao.selectByPage(page, limit);


    }

    @Override
    public List<User> selectLikeName(Object value) throws Exception {
        return userDao.selectLikeName(value);
    }

    @Override
    public User selectByPhone(String phone) throws Exception {
        List<User> users = userDao.selectByPhone(phone);

        if (users.size()>0){
            throw new BusinessException(ExceptionCode.DUPLICATE_PHONE);
        }
        return null;

    }
}
